package com.atguigu.hellomp;

import com.atguigu.hellomp.entity.User;
import com.atguigu.hellomp.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.function.Consumer;

/**
 * @author 张英杰
 * @date 2025/9/29/周一 20:25
 */
@SpringBootTest
public class T2 {

    @Autowired
    UserMapper userMapper;
    @Test
    public void a() {
        // 查询name=Tom的所有用户
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("name", "Tom");
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println(users);
        System.out.println("========1 查询name=Tom的所有用户========");
    }

    @Test
    public void b() {
        // 查询邮箱域名为baomidou.com的所有用户
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("email","baomidou.com");
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println(users);
        System.out.println("===============2 查询邮箱域名为baomidou.com的所有用户===============");
    }

    @Test
    public void c() {
        //查询所有用户信息并按照age字段降序排序
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.orderByDesc("age");
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println(users);
        System.out.println("===============3 查询所有用户信息并按照age字段降序排序===============");
    }

    @Test
    public void d(){
        // 查询age小于20或大于30的用户
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.ge("age",20).or().lt("age",30);
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println(users);
        System.out.println("===============4 查询age小于20或大于30的用户===============");
    }

    @Test
    public void e(){
        //邮箱域名为baomidou.com且年龄小于20或大于30且的用户
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("email","baomidou.com");
        userQueryWrapper.le("age",20).or().ge("age",30);

        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println(users);
        System.out.println("===============5 邮箱域名为baomidou.com且年龄小于20或大于30且的用户===============");
    }
    @Test
    public void f() {
        //邮箱域名为baomidou.com且年龄小于20或大于30且的用户
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.like("email", "baomidou.com").and((q) -> q.le("age", 20).or().ge("age", 30));
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println(users);
        System.out.println("===============5 邮箱域名为baomidou.com且年龄小于20或大于30且的用户===============");
    }


    @Test
    public void g(){
        //邮箱域名为baomidou.com且年龄小于20或大于30且的用户
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();

        Consumer<QueryWrapper<User>> queryWrapperConsumer = new Consumer<QueryWrapper<User>>() {
            @Override
            public void accept(QueryWrapper<User> userQueryWrapper) {   // lambda表达式
                userQueryWrapper.le("age",20).or().ge("age",30);
            }
        };
        userQueryWrapper.like("email","baomidou.com").and(queryWrapperConsumer);
        List<User> users = userMapper.selectList(userQueryWrapper);
        System.out.println(users);
        System.out.println("===============5 邮箱域名为baomidou.com且年龄小于20或大于30且的用户===============");
    }

    @Test
    public void h(){

        //邮箱域名为baomidou.com且年龄小于20或大于30且的用户
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getEmail,"baomidou.com");
        userLambdaQueryWrapper.le(User::getAge,20).or().ge(User::getAge,30);
        List<User> users = userMapper.selectList(userLambdaQueryWrapper);
        System.out.println(users);
        System.out.println("===============5 邮箱域名为baomidou.com且年龄小于20或大于30且的用户===============");

        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("name","tom");
        userUpdateWrapper.set("age",100);
        userMapper.update(null,userUpdateWrapper);
    }
}